#pragma once

#include <QtGui>

namespace
{

inline QToolButton* create_toolbutton(int btn_width, int btn_height)
{
    auto button = new QToolButton();

    button->setStyleSheet("QToolButton{"
                       "    background-color: rgb(255, 255, 255);"
                       "    border: 1px solid #000000;"
                       "}"
                       "QToolButton:checked{"
                       "    background-color: rgb(64, 64, 64);"
                       "    color: #FFFFFF;"
                       "}");

    button->setMinimumWidth (btn_width );
    button->setMinimumHeight(btn_height);

    auto font = button->font();
    font.setPixelSize(16);
    button->setFont(font);

    return button;
}

} // end of anonymous namespace

class taskbar
    : public QWidget
{
    Q_OBJECT

public:
    taskbar(QWidget *parent, bool standard)
        : QWidget  (parent)
        , standard_(standard)
        , layout_  (new QHBoxLayout())
    {
        setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
        setStyleSheet("QWidget {"
                      "    padding: 1px;"
                      "    background: white;"
                      "    spacing: 1px;"
                      "}");

        layout_->setContentsMargins(1, 1, 1, 1);
        layout_->setSpacing(1);
        setLayout(layout_);
    }

    static const int height    = 30;
    static const int btn_width = 109;

public:
    size_t add_button_named(std::string const &name, bool checkable)
    {
        auto btn = standard_ ? new QToolButton() : create_toolbutton(btn_width, height - 2);
        btn->setText(QString::fromUtf8(name.c_str()));

        return add_button(btn, checkable);
    }

    void set_checked(size_t button_id, bool checked)
    {
        QToolButton* btn = (QToolButton*)button_id ;
        if (buttons_.contains(btn))
        {
            btn->setCheckable(true) ;
            btn->setChecked(checked) ;
        }
    }


private:
    size_t add_button(QToolButton *btn, bool checkable)
    {
        layout_->addWidget(btn);
        btn->setCheckable(checkable) ;

        buttons_.insert(btn);

        return (size_t)btn ;
    }

    void resizeEvent(QResizeEvent *e) override
    {
        emit size_changed();
        QWidget::resizeEvent(e);
    }

Q_SIGNALS:
    void size_changed();

private:
    QSet<QToolButton*>   buttons_;
    bool                 standard_;
    QHBoxLayout         *layout_;
};



class main_window
        : public QMainWindow
{
    Q_OBJECT

public:
    explicit main_window(QWidget *parent = 0);
    void add_widget(QWidget *widget);

public Q_SLOTS:
    void set_fullscreen(bool full);

protected:
    void changeEvent    (QEvent       *e) override;
    void resizeEvent    (QResizeEvent *e) override;
    void mouseMoveEvent (QMouseEvent  *e) override;
    void moveEvent      (QMoveEvent   *e) override;

private Q_SLOTS:
    void move_taskbar();

private:
    taskbar *taskbar_;
    bool     full_screen_;

    QSet<QWidget*> childs_;
};

